<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { BaseinfoChatApi } from '#/api/csyt/baseinfochat';

import { Page, useVbenModal } from '@vben/common-ui';
import { message,Tabs } from 'ant-design-vue';
import Form from './modules/form.vue';
import NewForm from './modules/NewForm.vue';

import { ref, computed } from 'vue';
import { $t } from '#/locales';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { getBaseinfoChatPage, deleteBaseinfoChat, deleteBaseinfoChatList, exportBaseinfoChat,getBaseinfoChatInfo } from '#/api/csyt/baseinfochat';
import { downloadFileFromBlobPart, isEmpty } from '@vben/utils';
import { useGridColumns, useGridFormSchema } from './data';
import {useRouter} from "vue-router";


const [FormModal, formModalApi] = useVbenModal({
  connectedComponent: Form,
  destroyOnClose: true,
});

const [FormModal2, formModalApi2] = useVbenModal({
  connectedComponent: NewForm,
  destroyOnClose: true,
  fullscreen:true,
  footer:false,
  header:false,
  closable:false,
  fullscreenButton:false,
  closeOnPressEscape:false
});
// 点击会话编号
 function clickNo(row){
  formModalApi2.setData(row).open();
  // const data = await getBaseinfoChatInfo({chatNo:row.chatNo});
  // console.log(data)

}


/** 刷新表格 */
function onRefresh() {
  gridApi.query();
}

/** 创建会话信息 */
function handleCreate() {
  formModalApi.setData({}).open();
}

/** 编辑会话信息 */
function handleEdit(row: BaseinfoChatApi.BaseinfoChat) {
  formModalApi.setData(row).open();
}


/** 删除会话信息 */
async function handleDelete(row: BaseinfoChatApi.BaseinfoChat) {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting', [row.id]),
    key: 'action_key_msg',
  });
  try {
    await deleteBaseinfoChat(row.id as number);
    message.success({
      content: $t('ui.actionMessage.deleteSuccess', [row.id]),
      key: 'action_key_msg',
    });
    onRefresh();
  } finally {
    hideLoading();
  }
}

/** 批量删除会话信息 */
async function handleDeleteBatch() {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting'),
    key: 'action_key_msg',
  });
  try {
    await deleteBaseinfoChatList(checkedIds.value);
    checkedIds.value = [];
    message.success({
      content: $t('ui.actionMessage.deleteSuccess'),
      key: 'action_key_msg',
    });
    onRefresh();
  } finally {
    hideLoading();
  }
}

const checkedIds = ref<number[]>([])
function handleRowCheckboxChange({
  records,
}: {
  records: BaseinfoChatApi.BaseinfoChat[];
}) {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出表格 */
async function handleExport() {
  const data = await exportBaseinfoChat(await gridApi.formApi.getValues());
  downloadFileFromBlobPart({ fileName: '会话信息.xls', source: data });
}

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    schema: useGridFormSchema(),
  },
  gridOptions: {
    columns: useGridColumns(),
    height: 'auto',
    pagerConfig: {
      enabled: true,
    },
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getBaseinfoChatPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    toolbarConfig: {
      refresh: true,
      search: true,
    },
  } as VxeTableGridOptions<BaseinfoChatApi.BaseinfoChat>,
  gridEvents:{
      checkboxAll: handleRowCheckboxChange,
      checkboxChange: handleRowCheckboxChange,
  }
});
const router = useRouter();
/** 编辑需求管理 */
function handleEditStatus(row) {
  if (router) {
    router.push({
      path: '/gxgl/baseinfo-chathistory',
      query: {
        chatNo:row.chatNo,
        chatId:row.id,

      },
    });
  }
}
function handleEditHe(row) {
  if (router) {
    router.push({
      path: '/gxgl/baseinfo-contract',
      query: {
        chatNo:row.chatNo,
        chatId:row.id,

      },
    });
  }
}
</script>

<template>
  <Page auto-content-height>
    <FormModal @success="onRefresh" />
    <FormModal2 @success="onRefresh" />
    <Grid table-title="会话信息列表">
      <template #toolbar-tools>
        <TableAction
          :actions="[
            {
              label: $t('ui.actionTitle.create', ['会话信息']),
              type: 'primary',
              icon: ACTION_ICON.ADD,
              auth: ['csyt:baseinfo-chat:create'],
              onClick: handleCreate,
            },
            {
              label: $t('ui.actionTitle.export'),
              type: 'primary',
              icon: ACTION_ICON.DOWNLOAD,
              auth: ['csyt:baseinfo-chat:export'],
              onClick: handleExport,
            },
            {
              label: $t('ui.actionTitle.deleteBatch'),
              type: 'primary',
              danger: true,
              icon: ACTION_ICON.DELETE,
              disabled: isEmpty(checkedIds),
              auth: ['csyt:baseinfo-chat:delete'],
              onClick: handleDeleteBatch,
            },
          ]"
        />
      </template>
      <template #hui-url="{ row }">
        <span @click="clickNo(row)" style="cursor: pointer;">{{row.chatNo}}</span>
      </template>
      <template #actions="{ row }">
        <TableAction
          :actions="[
            {
              label: $t('common.edit'),
              type: 'link',
              icon: ACTION_ICON.EDIT,
              auth: ['csyt:baseinfo-chat:update'],
              onClick: handleEdit.bind(null, row),
            },
            {
              label: $t('common.delete'),
              type: 'link',
              danger: true,
              icon: ACTION_ICON.DELETE,
              auth: ['csyt:baseinfo-chat:delete'],
              popConfirm: {
                title: $t('ui.actionMessage.deleteConfirm', [row.id]),
                confirm: handleDelete.bind(null, row),
              },
            },

          ]"
          :drop-down-actions="[
            {
              label: '双方会话',
              type: 'link',
              ifShow: true,
              onClick: handleEditStatus.bind(null, row),
            },
            {
              label: '合同管理',
              type: 'link',
              ifShow: true,
              onClick: handleEditHe.bind(null, row),
            },
          ]"
        />
      </template>
    </Grid>

  </Page>
</template>
